package com.xinxing.city.recoverycheck.serialport;

import android.util.Log;

import com.jsjnx.serial.SerialPortManager;
import com.jsjnx.serial.listener.OnOpenSerialPortListener;
import com.xinxing.city.recoverycheck.DI;
import com.xinxing.city.recoverycheck.entitys.KernelAction;
import com.xinxing.city.recoverycheck.entitys.KernelActions;
import com.xinxing.city.recoverycheck.interfaces.IRfidManager;
import com.xinxing.city.recoverycheck.sdk_ttys.Hexs;
import com.xinxing.city.recoverycheck.sdk_ttys.ZtlTools;
import com.xinxing.city.recoverycheck.util.StringUtil;


import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class RfidzhiManager implements IRfidManager {
    static SerialPortManager serialPortManager3 = null;
    static boolean open = false;

    private List<String> rfids;
    private int count =0;
    @Override
    public void start(String sPort, int iBaudRate) {

        rfids = new ArrayList<>();

        if (sPort == null || sPort.length() < 5) {
            sPort = "/dev/ttyS1";// 垃圾箱串口
        }

        String DeviceVersion = ZtlTools.getDeviceMdeo();
        if (DeviceVersion.contains("3399")) {
            if (sPort.contains("ttyS3")) {
                sPort = "/dev/ttysWK0";
            }
            if (sPort.contains("ttyS1")) { // 读卡器串口
                sPort = "/dev/ttysWK2";
            }
        }


        int point2 = 9600;
        if (iBaudRate > 0) {
            point2 = iBaudRate;
        }
        int ps = 3;//默认使用3
        if (sPort.contains("ttyS1") || sPort.contains("ttysWK2")
            // || ttyS.equals("ttyS2")
        ) {
            ps = 1;
        }

        serialPortManager3 = new SerialPortManager(sPort, point2, SerialPortManager.SPLICING);

        serialPortManager3.setOnOpenSerialPortListener(new OnOpenSerialPortListener() {
                    @Override
                    public void onSuccess(File device) {
                        open = true;
                        System.out.println("【串口】：打开成功：0 ， ttyS：" + device.getAbsolutePath());
                    }

                    @Override
                    public void onFail(File device, Status status) {
                        open = false;
//                        CC.latest_received_ac_data_time = 1;
                        System.out.println("【串口】：打开失败：0 ， latest_received_ac_data_time：");
                    }
                })
                .setOnSerialPortDataListener(buffer -> {

                    if (buffer == null || buffer.length < 1) {
                        System.out.println("【收到串口1数据】：报文长度错误：0 ， ");
                        return;
                    }
                    read_package(buffer);

                }).start();

    }
    public void read_package(byte[] buffer) {

        String res2 = StringUtil.byte2HexStr(buffer);
        Log.e("rfid 读：", res2);
        if (res2.contains("7e")) {
            String[] res = res2.split("7e");
            count += res.length;
            Log.e("TTT","count"+count);
            if (res.length > 0) {
                for (String s : res) {
                    if (s.length() >= 30 && s.substring(0, 2).equals("bb") ) {
                        String strs = s.substring(16, 16 + 12);
                        if (!rfids.contains(strs)) {
                            rfids.add(strs);
                        }
                    }
                }
                if (rfids.size() > 0) {
                    EventBus.getDefault().post(new KernelAction(KernelActions.RFID_READ, null));
                }else if(count==5){
                    EventBus.getDefault().post(new KernelAction(KernelActions.RFID_READ_F, null));
                    Log.e("TTT","读卡失败");
                }
            }
        }
    }

    @Override
    public void stop() {
        serialPortManager3.closeSerialPort();
    }

    @Override
    public void singleRfid() {
//        Log.e("TTT","开始读取rfid");
//        ArrayList<Byte> mbuffer = new ArrayList<>();
//        mbuffer.add((byte) 0xBB);
//        mbuffer.add((byte) 0x00);
//        mbuffer.add((byte) 0x22);
//        mbuffer.add((byte) 0x00);
//        mbuffer.add((byte) 0x00);
//        mbuffer.add((byte) 0x22);
//        mbuffer.add((byte) 0x7E);
//
//        byte[] byteArray = new byte[mbuffer.size()];
//        for (int i = 0; i < mbuffer.size(); i++) {
//            byteArray[i] = mbuffer.get(i);
//        }
//        send_cmd(byteArray);
    }

    @Override
    public void loopRfid() {
        count = 0;
        rfids.clear();
        ArrayList<Byte> mbuffer = new ArrayList<>();
        mbuffer.add((byte) 0xBB);
        mbuffer.add((byte) 0x00);
        mbuffer.add((byte) 0x27);
        mbuffer.add((byte) 0x00);
        mbuffer.add((byte) 0x03);
        mbuffer.add((byte) 0x22);
        mbuffer.add((byte) 0x00);
        mbuffer.add((byte) 0x05);
        mbuffer.add((byte) 0x51);
        mbuffer.add((byte) 0x7E);
//        BB 00 27 00 03 22 27 10 83 7E

        byte[] byteArray = new byte[mbuffer.size()];
        for (int i = 0; i < mbuffer.size(); i++) {
            byteArray[i] = mbuffer.get(i);
        }
        send_cmd(byteArray);
    }

    @Override
    public void stopLoop() {
//        ArrayList<Byte> mbuffer = new ArrayList<>();
//        mbuffer.add((byte) 0xBB);
//        mbuffer.add((byte) 0x00);
//        mbuffer.add((byte) 0x28);
//        mbuffer.add((byte) 0x00);
//        mbuffer.add((byte) 0x00);
//        mbuffer.add((byte) 0x28);
//        mbuffer.add((byte) 0x7E);
//
//        byte[] byteArray = new byte[mbuffer.size()];
//        for (int i = 0; i < mbuffer.size(); i++) {
//            byteArray[i] = mbuffer.get(i);
//        }
//        send_cmd(byteArray);
    }

    @Override
    public List<String> getRfid() {
        return rfids;
    }

    public static void send_cmd(byte[] buffer) {
        byte[] bOutArray = (byte[]) buffer;
        if (bOutArray != null) {
            if (serialPortManager3 != null) {
                String hexId = Hexs.ByteToHex(bOutArray);
                boolean wrt = serialPortManager3.sendBytes(bOutArray);
            }
        }

        String res2 = StringUtil.byte2HexStr(buffer);
        Log.e("rfid 写：", res2);
    }
}
